<?php
/**
 * +----------------------------------------------------------------------
 * | [ DO WHAT YOU LOVE AND FUCK THE REST ]
 * +----------------------------------------------------------------------
 * | EMAIL: < 931274989@qq.com >
 * +----------------------------------------------------------------------
 * | WECHAT: xzc016
 * +----------------------------------------------------------------------
 * | 图片管理
 * +----------------------------------------------------------------------
 */
namespace app\admin\controller;

use app\common\model\Images as ImagesModel;

use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use think\facade\Config;

class Image extends Base
{
  // 图片列表
  public function index() {
    $model = new ImagesModel();
    $post = $this->request->param();
    
    if (isset($post['status']) and ($post['status'] == 1 or $post['status'] === '0' or $post['status'] == -1)) {
      $where[] = ['status', '=', $post['status']];
    }

    if(isset($post['create_time']) and !empty($post['create_time'])) {
      $min_time = strtotime($post['create_time']);
      $max_time = $min_time + 24 * 60 * 60;
      $where[] = ['create_time','between',[$min_time,$max_time]];
    }
    
    $images = empty($where) ? $model->order('create_time desc')->paginate(Config::get('admin.page_size')) : $model->where($where)->order('create_time desc')->paginate(Config::get('admin.page_size'), false,['query'=>request()->get()]);
    View::assign('images',$images);
    return View::fetch();
  }

  // 文件审核
  public function audit()
  {
    //获取文件id
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    if($id > 0) {
      if($this->request->isPost()) {
        //是提交操作
        $post = $this->request->post();
        $status = $post['status'];
        if(false == Db::name('images')->where('id',$id)->update(['status'=>$status])) {
        	return error('审核提交失败');
        } else {
          adminLog();//写入日志
          return success('审核提交成功','admin/image/index');
        }
      } 
    } else {
      return error('id不正确');
    }
  }

  // 图片下载
  public function download()
  {
    if($this->request->isAjax()) {
      $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
      if($id > 0) {
        //获取下载链接
        $data = Db::name('images')->where('id',$id)->find();
        $res['data'] = $data['url'];
        $res['name'] = basename($data['url']);
        //增加下载量
        Db::name('images')->where('id',$id)->inc('download')->update();
        $res['code'] = 1;
        adminLog();
        return json($res);
      } else {
        return error('错误请求');
      }
    }
  }

  // 图片删除
  public function delete()
  {
    if($this->request->isAjax()) {
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
      $image = Db::name('images')->where('id',$id)->value('url');
      if(file_exists(root_path() . 'public' . $image)) {
        if(unlink(root_path() . 'public' . $image)) {
          if(false == Db::name('images')->where('id',$id)->delete()) {
              return error('删除失败');
          } else {
            adminLog($id);//写入日志
            return success('删除成功','admin/image/index');
          }
        } else {
          return error('删除失败');
        }
      } else {
        if(false == Db::name('images')->where('id',$id)->delete()) {
          return error('删除失败');
        } else {
          adminLog($id);//写入日志
          return success('删除成功','admin/image/index');
        }
      }
    }
  }

}
?>